﻿using System.ServiceModel;
using System.Text;
using System.Windows.Browser;
using Objectware.Silverlight.Testing.TestProject.TestResultService;


namespace Objectware.Silverlight.Testing.TestProject
{
	public class ServiceLocator
	{
		public static readonly ServiceLocator Default = new ServiceLocator();

		private ServiceLocator()
		{
			
		}

		private string GetBaseUrl()
		{
			var uri = HtmlPage.Document.DocumentUri;

			var builder = new StringBuilder();
			builder.Append(uri.Scheme);
			builder.Append("://");
			builder.Append(uri.Host);
			builder.Append(string.Format(":{0}", uri.Port));
			return builder.ToString();
		}


		public T GetInstance<T>()
			where T:class
		{
			if( typeof(T).Equals(typeof(TestResultService.TestResultService)) )
			{
				var binding = new BasicHttpBinding();

				var address = new EndpointAddress(string.Format("{0}/Services/TestResultService.svc", this.GetBaseUrl()));
				return new TestResultServiceClient(binding,address) as T;
			}
			return null;
		}
	}
}